/**
 * 
 */
package com.nari.baseinfo.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IConcentrator;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.model.IShareArea;
import com.nari.baseinfo.service.IBaseStatisService;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IConcentratorService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.baseinfo.service.IShareAreaService;
import com.nari.baseinfo.utils.BaseStatisConstants;
import com.nari.baseinfo.utils.statisentity.IGenericStatisEntity;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class ConcentratorAction extends JSONActionSupport {
	private static final long serialVersionUID = 7418159399238388018L;
	private IConcentratorService iConcentratorService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IShareAreaService iShareAreaService;
	private IBaseStatisService iBaseStatisService;
	//集中器信息查询
	private String projectUID;
	private String communityUID;
	private String buildingUID;
	private String concentratorNo;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IConcentrator> concentratorPage;
	private Map<String, String> pageProjectMap; //所属项目id-projectName映射表
	private Map<String, String> pageCommunityMap; //所属小区id-communityName映射表
	//集中器信息管理
	private String concentratorUID;
	private IConcentrator concentrator;
	private List<IShareArea> shareAreaList;
	//集中器列表查询
	private String shareAreaUID;
	private List<IConcentrator> concentratorList;
	//设置集中器所属的分摊域
	private IShareArea shareArea;
	private String concentratorIDList;
	private IProject project;
	private ICommunity community;
	private IBuilding building;
	//统计信息
	private IGenericStatisEntity statisEntity;
	
	/**
	 * 分页查询集中器信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-15
	 */
	public String queryConcentratorPage() throws Exception {
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		// 所属楼栋列表
				if ((this.projectUID != null && !"".equals(projectUID))
						|| (this.communityUID != null && !"".equals(communityUID))) {
					this.buildingList = this.iBuildingService
							.queryBuildingByBelongings(projectUID, communityUID);
				} else {
					this.buildingList = new ArrayList<IBuilding>();
				}
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.concentratorPage = this.iConcentratorService.queryConcentratorPage(projectUID, communityUID, buildingUID,concentratorNo, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.concentratorPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.concentratorPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//项目映射表
		this.pageProjectMap = new HashMap<String, String>();
		for(IProject project: this.projectList) {
			this.pageProjectMap.put(project.getId(), project.getProjectName());
		}
		//小区映射表
		this.pageCommunityMap = this.iConcentratorService.queryReferCommunityMap(concentratorPage.getList());
		return "cencentratorPage";
	}
	
	/**
	 * 集中器信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-15
	 */
	public String queryConcentratorForManage() throws Exception {
		//集中器信息
		if(this.concentratorUID!=null && !"".equals(concentratorUID)) {
			this.concentrator = this.iConcentratorService.queryEntityById(concentratorUID);
			this.projectUID = this.concentrator.getProjectID();
			this.communityUID = this.concentrator.getCommunityID();
		}
		//所属项目列表信息 
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		// 所属楼栋列表
		if((this.projectUID != null && !"".equals(projectUID))
				|| (this.communityUID != null && !"".equals(communityUID))) {
			this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		} else {
			this.buildingList = new ArrayList<IBuilding>();
		}
		return "concentratorManage";
	}
	
	/**
	 * 新增或修改集中器信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-15
	 */
	public String saveOrUpdateConcentrator() throws Exception {
		if(this.concentrator.getId()!=null && !"".equals(concentrator.getId())) {
			this.iConcentratorService.updateEntity(concentrator, concentrator.getId());
		} else {
			this.iConcentratorService.saveEntity(concentrator);
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据集中器唯一标识删除集中器信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-15
	 */
	public String deleteConcentratorByUID() throws Exception {
		this.rmsg = this.iConcentratorService.deleteCascadeByConcentratorUID(concentratorUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询集中器详细资料
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-27
	 */
	public String queryConcentratorDetail() throws Exception {
		this.concentrator = this.iConcentratorService.queryEntityById(concentratorUID);
		if(concentrator != null) {
			//所属项目
			this.project = iProjectService.queryEntityById(concentrator.getProjectID());
			//所属小区
			this.community = iCommunityService.queryEntityById(concentrator.getCommunityID());
			//所属楼栋
			if(concentrator.getBuildingID()!=null && !"".equals(concentrator.getBuildingID())) {
				this.building = iBuildingService.queryEntityById(concentrator.getBuildingID());
			}
			//统计信息
			this.statisEntity = iBaseStatisService.queryBaseStatisEntity(BaseStatisConstants.statisType_concentratorDetail, concentratorUID);
		}
		return "concentratorDetail";
	}
	
	/**
	 * 根据所属关系查询集中器列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String queryConcentratorsByBelongs() throws Exception {
		this.concentratorList = this.iConcentratorService.queryConcentratorsByBelongs(projectUID, communityUID, shareAreaUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.concentratorList));
		return Action.SUCCESS;
	}
	 
	/**
	 * 根据所属分摊域查询集中器列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	public String queryConcentratorPageByAreaUID() throws Exception {
		if(shareAreaUID!=null && !"".equals(shareAreaUID)) {
			//分摊域信息
			this.shareArea = this.iShareAreaService.queryEntityById(shareAreaUID);
			//分摊域所属项目、小区
			this.projectUID = this.shareArea.getProjectID();
			this.communityUID = this.shareArea.getCommunityID();
		}
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.concentratorPage = this.iConcentratorService.queryConcentratorPageByAreaUID(shareAreaUID, projectUID, communityUID, concentratorNo, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.concentratorPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.concentratorPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		return "areaConcentratorPage";
	}
	
	/**
	 * 分页查询不在某一分摊域下的集中器列表(用于设置集中器所属的分摊域)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	public String queryConcentratorPageForAreaAssign() throws Exception {
		if(shareAreaUID!=null && !"".equals(shareAreaUID)) {
			//分摊域信息
			this.shareArea = this.iShareAreaService.queryEntityById(shareAreaUID);
			//分摊域所属项目、小区
			this.projectUID = this.shareArea.getProjectID();
			this.communityUID = this.shareArea.getCommunityID();
		}
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.concentratorPage = this.iConcentratorService.queryConcentratorPageExceptAreaUID(shareAreaUID, projectUID, communityUID, concentratorNo, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.concentratorPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.concentratorPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//项目映射表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		this.pageProjectMap = new HashMap<String, String>();
		for(IProject project: this.projectList) {
			this.pageProjectMap.put(project.getId(), project.getProjectName());
		}
		//小区映射表
		this.pageCommunityMap = this.iConcentratorService.queryReferCommunityMap(concentratorPage.getList());
		return "areaConcentratorAssign";
	}
	
	/**
	 * 设置集中器所属分摊域
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	public String assignConcentratorArea() throws Exception {
		this.rmsg = this.iConcentratorService.saveConcentratorArea(shareAreaUID, concentratorIDList);
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 取消集中器所属分摊域设置
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	public String cancelConcentratorRoomByUID() throws Exception {
		this.concentrator = this.iConcentratorService.queryEntityById(concentratorUID);
//		this.concentrator.setShareAreaID(null);
		this.iConcentratorService.updateEntity(concentrator, concentratorUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iConcentratorService
	 */
	@JSON(serialize=false)
	public IConcentratorService getiConcentratorService() {
		return iConcentratorService;
	}
	
	/**
	 * @param iConcentratorService the iConcentratorService to set
	 */
	public void setiConcentratorService(IConcentratorService iConcentratorService) {
		this.iConcentratorService = iConcentratorService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iShareAreaService
	 */
	@JSON(serialize=false)
	public IShareAreaService getiShareAreaService() {
		return iShareAreaService;
	}

	/**
	 * @param iShareAreaService the iShareAreaService to set
	 */
	public void setiShareAreaService(IShareAreaService iShareAreaService) {
		this.iShareAreaService = iShareAreaService;
	}

	/**
	 * @return the iBaseStatisService
	 */
	@JSON(serialize=false)
	public IBaseStatisService getiBaseStatisService() {
		return iBaseStatisService;
	}

	/**
	 * @param iBaseStatisService the iBaseStatisService to set
	 */
	public void setiBaseStatisService(IBaseStatisService iBaseStatisService) {
		this.iBaseStatisService = iBaseStatisService;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the concentratorNo
	 */
	public String getConcentratorNo() {
		return concentratorNo;
	}

	/**
	 * @param concentratorNo the concentratorNo to set
	 */
	public void setConcentratorNo(String concentratorNo) {
		this.concentratorNo = concentratorNo;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the concentratorPage
	 */
	public ResultWithPageCond<IConcentrator> getConcentratorPage() {
		return concentratorPage;
	}

	/**
	 * @param concentratorPage the concentratorPage to set
	 */
	public void setConcentratorPage(
			ResultWithPageCond<IConcentrator> concentratorPage) {
		this.concentratorPage = concentratorPage;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the pageCommunityMap
	 */
	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	/**
	 * @param pageCommunityMap the pageCommunityMap to set
	 */
	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	/**
	 * @return the concentratorUID
	 */
	public String getConcentratorUID() {
		return concentratorUID;
	}

	/**
	 * @param concentratorUID the concentratorUID to set
	 */
	public void setConcentratorUID(String concentratorUID) {
		this.concentratorUID = concentratorUID;
	}

	/**
	 * @return the concentrator
	 */
	public IConcentrator getConcentrator() {
		return concentrator;
	}

	/**
	 * @param concentrator the concentrator to set
	 */
	public void setConcentrator(IConcentrator concentrator) {
		this.concentrator = concentrator;
	}

	/**
	 * @return the shareAreaList
	 */
	public List<IShareArea> getShareAreaList() {
		return shareAreaList;
	}

	/**
	 * @param shareAreaList the shareAreaList to set
	 */
	public void setShareAreaList(List<IShareArea> shareAreaList) {
		this.shareAreaList = shareAreaList;
	}

	/**
	 * @return the shareAreaUID
	 */
	public String getShareAreaUID() {
		return shareAreaUID;
	}

	/**
	 * @param shareAreaUID the shareAreaUID to set
	 */
	public void setShareAreaUID(String shareAreaUID) {
		this.shareAreaUID = shareAreaUID;
	}

	/**
	 * @return the concentratorList
	 */
	public List<IConcentrator> getConcentratorList() {
		return concentratorList;
	}

	/**
	 * @param concentratorList the concentratorList to set
	 */
	public void setConcentratorList(List<IConcentrator> concentratorList) {
		this.concentratorList = concentratorList;
	}

	/**
	 * @return the shareArea
	 */
	public IShareArea getShareArea() {
		return shareArea;
	}

	/**
	 * @param shareArea the shareArea to set
	 */
	public void setShareArea(IShareArea shareArea) {
		this.shareArea = shareArea;
	}

	/**
	 * @return the concentratorIDList
	 */
	public String getConcentratorIDList() {
		return concentratorIDList;
	}

	/**
	 * @param concentratorIDList the concentratorIDList to set
	 */
	public void setConcentratorIDList(String concentratorIDList) {
		this.concentratorIDList = concentratorIDList;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the building
	 */
	public IBuilding getBuilding() {
		return building;
	}

	/**
	 * @param building the building to set
	 */
	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	/**
	 * @return the statisEntity
	 */
	public IGenericStatisEntity getStatisEntity() {
		return statisEntity;
	}

	/**
	 * @param statisEntity the statisEntity to set
	 */
	public void setStatisEntity(IGenericStatisEntity statisEntity) {
		this.statisEntity = statisEntity;
	}

	public String getBuildingUID() {
		return buildingUID;
	}

	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	
}
